package com.fqgj.sentry.biz.mc;

import com.fqgj.sentry.biz.mc.vo.role.RoleVo;
import com.fqgj.sentry.common.cache.RequestLocalInfo;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.controller.mc.request.AddRoleVo;
import com.fqgj.sentry.manage.dao.McMenuDao;
import com.fqgj.sentry.manage.dao.McPermissionDao;
import com.fqgj.sentry.manage.domain.PermissionMenuInfo;
import com.fqgj.sentry.manage.entity.McMenuEntity;
import com.fqgj.sentry.manage.entity.McPermissionEntity;
import com.fqgj.sentry.manage.entity.McRoleEntity;
import com.fqgj.sentry.manage.entity.McRolePermissionEntity;
import com.fqgj.sentry.manage.service.RolePermissionService;
import com.fqgj.sentry.manage.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-17 下午3:14
 */

@Component
public class RoleBusiness {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private McPermissionDao permissionDao;
    @Autowired
    private McMenuDao menuDao;

    public ApiPage queryRoleListForPage(Long merchantId, ApiPage page) {
        List<McRoleEntity> roleEntityList = roleService.selectByMerchantForPage(merchantId, page);
        List<RoleVo> roleVoList = new ArrayList<>();
        for (McRoleEntity roleEntity : roleEntityList) {
            RoleVo roleVo = new RoleVo()
                    .setId(roleEntity.getId())
                    .setMerchantId(roleEntity.getMerchantId())
                    .setName(roleEntity.getName());
            //下面有的权限集合
            roleVoList.add(roleVo);
        }
        page.setData(roleVoList);
        return page;
    }

    /**
     * 新增 角色权限
     *
     * @param addRoleVo
     * @return
     */
    public Boolean addRolePermission(AddRoleVo addRoleVo) {
        McRoleEntity roleEntity = new McRoleEntity();
        roleEntity.setMerchantId(RequestLocalInfo.getCurrentMerchantId())
                .setName(addRoleVo.getRoleName());
        roleEntity = roleService.insert(roleEntity);
        Long[] permissionIds = addRoleVo.getPermissionIds();
        if (permissionIds != null) {
            for (Long permissionId : permissionIds) {
                McRolePermissionEntity rolePermissionEntity = new McRolePermissionEntity();
                rolePermissionEntity.setRoleId(roleEntity.getId())
                        .setPermissionId(permissionId);
                rolePermissionService.insert(rolePermissionEntity);
            }
        }
        return true;
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    public Integer delete(Long roleId) {
        return roleService.deleteRole(roleId);
    }

    /**
     * 修改 角色权限
     *
     * @param addRoleVo
     * @return
     */
    public Boolean updateRolePermission(AddRoleVo addRoleVo) {
        McRoleEntity roleEntity = new McRoleEntity();
        if (StringUtils.isNotEmpty(addRoleVo.getRoleName())) {
            roleEntity.setName(addRoleVo.getRoleName())
                    .setId(addRoleVo.getRoleId());
            roleService.update(roleEntity);
        }
        //删除 该角色下的所有权限
        rolePermissionService.deleteByRoleId(addRoleVo.getRoleId());
        Long[] permissionIds = addRoleVo.getPermissionIds();
        if (permissionIds != null) {
            for (Long permissionId : permissionIds) {
                McRolePermissionEntity rolePermissionEntity = new McRolePermissionEntity();
                rolePermissionEntity.setRoleId(roleEntity.getId())
                        .setPermissionId(permissionId);
                rolePermissionService.insert(rolePermissionEntity);
            }
        }
        return true;
    }

    /**
     * 角色 权限 菜单 查询
     *
     * @param roleId
     * @return
     */
    public List<PermissionMenuInfo> findListByRoleId(Long roleId) {
        List<PermissionMenuInfo> permissionMenuInfoList = new ArrayList<>();
        List<McRolePermissionEntity> rolePermissionEntityList = rolePermissionService.findListByRoleId(roleId);
        for (McRolePermissionEntity rolePermissionEntity : rolePermissionEntityList) {
            PermissionMenuInfo permissionMenuInfo = new PermissionMenuInfo();
            permissionMenuInfo.setPermissionId(rolePermissionEntity.getPermissionId());
            //权限表
            McPermissionEntity permissionEntity = permissionDao.selectByPrimaryKey(rolePermissionEntity.getPermissionId());
            permissionMenuInfo.setMenuId(permissionEntity.getMenuId());
            //菜单
            McMenuEntity menuEntity = menuDao.selectByPrimaryKey(permissionEntity.getMenuId());
            permissionMenuInfo.setPid(menuEntity.getPid());
            permissionMenuInfoList.add(permissionMenuInfo);
        }
        return permissionMenuInfoList;
    }


}
